package com.example.learnspringboot05web.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
public class LoginController {

    //    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    @PostMapping(value = "/user/login")
    public String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            Map<String, Object> mapMessage,
            HttpSession httpSession) {

        if (!StringUtils.isEmpty(username) && "123456".equals(password)){
            //登录成功
            //dashboard.html
//            return "dashboard";

            httpSession.setAttribute("loginUser", username);

            //防止表单重复提交，可以重定向到主页面
            return "redirect:/main.html";
        }

        mapMessage.put("msg", "用户名或密码错误");
        //登录失败
        return "login";


    }
}
